package com.feng.leetcode;

import java.util.LinkedHashMap;
import java.util.Map;

public class Thirteenth {
    public static void main(String[] args) {
        System.out.println(romanToInt("X"));
    }

    public static int romanToInt(String s) {
        Map<String,Integer> relation = new LinkedHashMap<>();
        relation.put("M",1000);
        relation.put("CM",900);
        relation.put("D",500);
        relation.put("CD",400);
        relation.put("C",100);
        relation.put("XC",90);
        relation.put("L",50);
        relation.put("XL",40);
        relation.put("X",10);
        relation.put("IX",9);
        relation.put("V",5);
        relation.put("IV",4);
        relation.put("I",1);

        int ans = 0;
        while (s.length()>0){
            for (String key : relation.keySet()) {
                if (s.startsWith(key)){
                    ans = ans + relation.get(key);
                    s = s.substring(key.length());
                }
            }
        }
        return ans;
    }
}
